package com.itic.demo.util;

import java.util.Map;

import org.apache.log4j.Logger;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;

public class JPushManager {  
	  
    // 日志  
    private static final Logger log = Logger.getLogger(JPushManager.class);  
    // 推送客户端  
    private static JPushClient buyerJpushClient = null;  
  
    static {  
        String buyerAppKey = null; // 推送app key  
        String buyerMasterSecret = null;// 推送app主密码  
        // int buyerMaxRetryTimes = 3;// 推送最大重发次数  
  
        buyerAppKey = "********";  
        buyerMasterSecret = "***********";  
        ClientConfig config = ClientConfig.getInstance();  
        config.setMaxRetryTimes(5);  
        config.setApnsProduction(false);    // development env  
        config.setTimeToLive(60 * 60 * 24);  
        buyerJpushClient = new JPushClient(buyerMasterSecret, buyerAppKey,  
                null, config);  
    }  
  
    public static void sendClient(JPushData pushData,  
            Map<String, String> extraMap) {  
        PushPayload payload = buildPushObject_all_alias_alert(pushData,  
                extraMap);  
        try {  
            PushResult result = buyerJpushClient.sendPush(payload);  
            log.info("Got result - " + result);  
        } catch (APIConnectionException e) {  
            log.error("Connection error, should retry later", e);  
        } catch (APIRequestException e) {  
            log.error("Should review the error, and fix the request", e);  
            log.info("HTTP Status: " + e.getStatus());  
            log.info("Error Code: " + e.getErrorCode());  
            log.info("Error Message: " + e.getErrorMessage());  
        }  
    }  
  
    public static PushPayload buildPushObject_all_alias_alert(  
            JPushData pushData, Map<String, String> extraMap) {  
        Message message = Message.newBuilder().setMsgContent(pushData.getContent())  
                .setTitle(pushData.getTitle()).setContentType(pushData.getTag()).build();  
        return PushPayload.newBuilder().setPlatform(Platform.all())  
                .setAudience(Audience.registrationId(pushData.getAlias()))  
//              .setNotification(Notification.alert(pushData.getContent()))//通知  
                .setMessage(message)//使用自定义消息推送  
                .build();  
    }  
}  
